package com.liusy.serachengine.demo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.UUID;

import org.apache.log4j.Logger;

/**
 * 
 * 已经处理过的ID列表
 */
public class ProcessedIds {

	private static Logger log = Logger.getLogger(ProcessedIds.class);

	/**
	 * 新增的ID列表
	 */
	private ArrayList<String> insertList;

	/**
	 * 更新的ID列表
	 */
	private ArrayList<String> updateList;

	/**
	 * 删除的ID列表
	 */
	private ArrayList<String> deleteList;

	/**
	 * 新增ID列表下标
	 */
	private volatile int insertIndex;

	/**
	 * 更新ID列表下标
	 */
	private volatile int updateIndex;

	/**
	 * 删除ID列表下标
	 */
	private volatile int deleteIndex;

	/**
	 * 每次取数据的最大值, 等于 INDEXING_RECORD_MAX
	 */
	private final int maxSize;

	/**
	 * 保存新增ID的文件名
	 */
	private final String insertFileName;

	/**
	 * 保存删除ID的文件名
	 */
	private final String deleteFileName;

	/**
	 * 
	 * @param source
	 *            资源ID
	 */
	public ProcessedIds(String source) {
		this.insertFileName = "../logs/" + source + ".insert";
		this.deleteFileName = "../logs/" + source + ".delete";

		//Configuration config = Configuration.getConfiguration();
		//maxSize = Integer.parseInt(config.getProperty("INDEXING_RECORD_MAX", "1000").toString());
		maxSize=0;
		insertIndex = 0;
		updateIndex = 0;
		deleteIndex = 0;

		insertList = new ArrayList<String>(maxSize);
		updateList = new ArrayList<String>(maxSize);
		deleteList = new ArrayList<String>(maxSize);

		try {
			File file = new File(insertFileName);
			if (file.exists()) {
				log.info("加载 [处理过的ID文件 - " + insertFileName + "]");
				BufferedReader in = new BufferedReader(new FileReader(file));
				for (String line = in.readLine(); line != null; line = in.readLine()) {
					insertList.add(line);
				}
				in.close();
			}  else {
				log.info("不存在 [处理过的ID文件 - " + insertFileName + "]");
			}

			file = new File(deleteFileName);
			if (file.exists()) {
				log.info("加载 [处理过的ID文件 - " + deleteFileName + "]");
				BufferedReader in = new BufferedReader(new FileReader(file));
				for (String line = in.readLine(); line != null; line = in.readLine()) {
					insertList.add(line);
				}
				in.close();
			} else {
				log.info("不存在 [处理过的ID文件 - " + deleteFileName + "]");
			}
		} catch (Exception e) {
			log.error("加载已经处理过的ID失败：" + e.getMessage(), e);
		}

		if (insertList.size() < maxSize) {
			int size = maxSize - insertList.size();
			for (int i = 0; i < size; i++) {
				insertList.add(UUID.randomUUID().toString());
			}
		}

		for (int i = 0; i < maxSize; i++) {
			updateList.add(UUID.randomUUID().toString());
		}

		if (deleteList.size() < maxSize) {
			int size = maxSize - deleteList.size();
			for (int i = 0; i < size; i++) {
				deleteList.add(UUID.randomUUID().toString());
			}
		}

		Runtime.getRuntime().addShutdownHook(new Thread() {
			public void run() {
				saveToFile();
			}
		});
	}

	/**
	 * 增加已经处理过的ID
	 * 
	 * @param id
	 */
	public void addInsertId(String id) {
		if (insertIndex >= maxSize) {
			insertIndex = 0;
		}
		insertList.set(insertIndex, id);
		insertIndex = insertIndex + 1;
	}

	public void addUpdateId(String id) {
		if (updateIndex >= maxSize) {
			updateIndex = 0;
		}
		updateList.set(updateIndex, id);
		updateIndex = updateIndex + 1;
	}

	public void addDeleteId(String id) {
		if (deleteIndex >= maxSize) {
			deleteIndex = 0;
		}
		deleteList.set(deleteIndex, id);
		deleteIndex = deleteIndex + 1;
	}

	/**
	 * ID 是否已经处理过
	 * 
	 * @param id
	 * @return
	 */
	public boolean containInsertId(String id) {
		return insertList.contains(id);
	}

	/**
	 * ID 是否已经处理过
	 * 
	 * @param id
	 * @return
	 */
	public boolean containUpdateId(String id) {
		return updateList.contains(id);
	}

	/**
	 * ID 是否已经处理过
	 * 
	 * @param id
	 * @return
	 */
	public boolean containDeleteId(String id) {
		return deleteList.contains(id);
	}

	/**
	 * 清空增加ID
	 */
	public void clearInsertId() {
		insertIndex = 0;
		for (int i = 0; i < maxSize; i++) {
			addInsertId(UUID.randomUUID().toString());
		}
	}
	
	/**
	 * 清空更新ID
	 */
	public void clearUpdateId() {
		updateIndex = 0;
		for (int i = 0; i < maxSize; i++) {
			addUpdateId(UUID.randomUUID().toString());
		}
	}
	
	/**
	 * 清空更新ID
	 */
	public void clearDeleteId() {
		deleteIndex = 0;
		for (int i = 0; i < maxSize; i++) {
			addDeleteId(UUID.randomUUID().toString());
		}
	}

	/**
	 * 把ID保存到硬盘文件
	 */
	public void saveToFile() {
		
		try {
			log.info("把处理过的ID保存到硬盘文件 - " + insertFileName);
			PrintWriter out = new PrintWriter(new FileWriter(this.insertFileName));
			for (int i = 0; i < insertList.size(); i++) {
				out.println(insertList.get(i));
			}
			out.flush();
			out.close();
		} catch (IOException e) {
			log.error("保存已经处理过的ID失败: " + e.getMessage());
		}

		
		try {
			log.info("把处理过的ID保存到硬盘文件 - " + deleteFileName);
			PrintWriter out = new PrintWriter(new FileWriter(this.deleteFileName));
			for (int i = 0; i < deleteList.size(); i++) {
				out.println(deleteList.get(i));
			}
			out.flush();
			out.close();
		} catch (IOException e) {
			log.error("保存已经处理过的ID失败: " + e.getMessage());
		}
	}

}
